package com.artron.mediaartron.ui.fragment.made;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.artron.baselib.base.BaseStaticFragment;
import com.artron.baselib.entity.Response;
import com.artron.mediaartron.R;
import com.artron.mediaartron.data.entity.Works;
import com.artron.mediaartron.data.net.NetConstant;
import com.artron.mediaartron.data.net.RetrofitHelper;
import com.artron.mediaartron.data.production.SuitableSizeHelper;
import com.artron.mediaartron.data.task.AddShoppingCartTask;
import com.artron.mediaartron.ui.activity.GeneralActivity;
import com.artron.mediaartron.ui.fragment.factory.HomeFragmentFactory;
import com.artron.mediaartron.ui.helper.ThrottleFirstClickHelper;
import com.jakewharton.rxbinding.view.RxView;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-10-13.
 */

public class AddVideoFragment extends BaseStaticFragment implements View.OnClickListener {

    private static final String TAG = "AddVideoFragment";

    @BindView(R.id.AddVideoFragment_fl_add)
    protected FrameLayout mFlAdd;
    @BindView(R.id.AddVideoFragment_tv_content)
    protected TextView mTvContent;
    private BottomSheetDialog mDialogAddVideo;
    private BottomSheetDialog mDialogBuy;
    private Works mWorks;

    public static AddVideoFragment newInstance() {
        Bundle args = new Bundle();
        AddVideoFragment fragment = new AddVideoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initVariable() {
        SuitableSizeHelper helper = SuitableSizeHelper.getHelper();
        mWorks = helper.getWorks();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_add_video;
    }

    @Override
    protected void initView() {
        mFlAdd.setOnClickListener(this);
        mTvContent.setOnClickListener(this);

        //视频相关的底部弹框
        mDialogAddVideo = new BottomSheetDialog(mContext);
        mDialogAddVideo.setContentView(R.layout.layout_bottom_add_video);
        mDialogAddVideo.findViewById(R.id.LayoutBottomAddVideo_tv_shot).setOnClickListener(this);
        mDialogAddVideo.findViewById(R.id.LayoutBottomAddVideo_tv_video).setOnClickListener(this);

        //购买相关的底部弹框
        mDialogBuy = new BottomSheetDialog(mContext);
        mDialogBuy.setContentView(R.layout.layout_bottom_add_shopping_cart_or_buy);

        View btnShoppingCart = mDialogBuy.findViewById(R.id.ItemBottomAddVideo_tv_add_shopping_cart);
        if (btnShoppingCart != null) {
            ThrottleFirstClickHelper.throttleFirst(btnShoppingCart, this);
        }

        View btnBuy = mDialogBuy.findViewById(R.id.ItemBottomAddVideo_tv_buy);
        if (btnBuy != null) {
            ThrottleFirstClickHelper.throttleFirst(btnBuy, this);
        }

        View btnCancel = mDialogBuy.findViewById(R.id.ItemBottomAddVideo_tv_cancel);
        if (btnCancel != null) {
            btnCancel.setOnClickListener(this);
        }

        mDialogBuy.getDelegate().findViewById(android.support.design.R.id.design_bottom_sheet)
                .setBackgroundResource(android.R.color.transparent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.AddVideoFragment_fl_add:
                mDialogAddVideo.show();
                break;
            case R.id.AddVideoFragment_tv_content:
                String text = mTvContent.getText().toString();
                if (!TextUtils.isEmpty(text)) {

                }
                GeneralActivity.startForResult(mContext, GeneralActivity.TEXT_EDIT, "文字编辑");
                break;
            case R.id.LayoutBottomAddVideo_tv_shot:

                break;
            case R.id.LayoutBottomAddVideo_tv_video:

                break;
            case R.id.ItemBottomAddVideo_tv_add_shopping_cart:
                addShoppingCart();
                break;
            case R.id.ItemBottomAddVideo_tv_buy:
                GeneralActivity.start(mContext, GeneralActivity.ORDER, "确认订单");
                break;
            case R.id.ItemBottomAddVideo_tv_cancel:
                mDialogBuy.dismiss();
                break;
        }
    }

    private void addShoppingCart() {
        Log.d(TAG, "call: " + mWorks);
        Observable<Response> api = AddShoppingCartTask.addShoppingCart(mWorks);
        RetrofitHelper.subscript(api, new Action1<Response>() {
            @Override
            public void call(Response response) {
                Log.d(TAG, "call: " + response);
                HomeFragmentFactory.getInstance().updateShoppingCart();
                GeneralActivity.start(mContext, GeneralActivity.SHOPPING_CART,
                        "购物车");
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_help:
                GeneralActivity.start(mContext, GeneralActivity.HELP, "云图介绍",
                        NetConstant.HELP_MAIN);
                break;
            case R.id.menu_skip:
                mDialogBuy.show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    //TODO
    public void setText(String text) {
        mTvContent.setText(text);
    }

}
